package com.example.controller.web;

import cn.hutool.extra.mail.MailUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.common.Result;
import com.example.component.JWTUtils;
import com.example.entity.dto.UserDTO;
import com.example.entity.user.Account;
import com.example.entity.chat.AiChat;
import com.example.entity.user.User;
import com.example.entity.user.mailAccount;
import com.example.service.AdminService;
import com.example.service.ChatAiService;
import com.example.service.UserService;
import com.example.service.weChatService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

@CrossOrigin
@RestController
@RequestMapping("/api")
public class WebController {

    @Resource
    private AdminService adminService;

    @Resource
    private UserService userService;

    @Resource
    private ChatAiService chatAiService;

    @Resource
    private weChatService wechatService;

    @Resource
    public JWTUtils jwtUtils;

    //    发送邮箱验证码
    @PostMapping("/send")
    public Result send(@RequestBody mailAccount mailaccount) {
        System.out.println("我是邮箱地址");
        System.out.println(mailaccount.getMailAddress());
        Random random = new Random();
        int code = random.nextInt(90000) + 10000; // 生成10000到99999之间的随机数
        MailUtil.send(mailaccount.getMailAddress(), "【吱吱农场验证码】", String.valueOf(code), false);
        userService.save_code(mailaccount.getMailAddress(), String.valueOf(code));
        return Result.success();
    }


    /**
     * 默认请求接口
     */
    @GetMapping("/")
    public Result hello() {
        return Result.success();
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result login(@RequestBody Account account) {
        Account ac = null;
        String uid=null;
        if ("ADMIN".equals(account.getRole())) {
            System.out.println("管理员登录");
            uid = adminService.login(account);
        } else if ("USER".equals(account.getRole())) {
            System.out.println("用户登录");
            uid = adminService.login(account);
        } else if ("FARMER".equals(account.getRole())) {
            System.out.println("农民登录");
            uid = adminService.login(account);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("token", jwtUtils.getJWT(account.getUsername()));
        jsonObject.put("uid", uid);
        return Result.success(jsonObject);
    }


    /**
     * 注册
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        userService.register(user);
        return Result.success();
    }


    /**
     * 邮箱登录
     */
    @PostMapping("/email_login")
    public Result email_login(@RequestBody mailAccount mailaccount) {
        mailAccount ac = null;
        System.out.println("用户邮箱登录");
        System.out.println(mailaccount);
        ac = userService.email_login(mailaccount);
        return Result.success(ac);
    }

    /**
     * 邮箱注册
     */
    @PostMapping("/email_register")
    public Result email_register(@RequestBody mailAccount mailaccount) {
        userService.email_register(mailaccount);
        return Result.success();
    }

    /**
     * 修改密码
     */
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account account) {
        if ("ADMIN".equals(account.getRole())) {
            adminService.updatePassword(account);
        }
        return Result.success();
    }

    //智能AI聊天
    @PostMapping("/chatAI")
    public Result chatWithGPT(@RequestBody AiChat aiChat) {
        String ac = null;
        ac = chatAiService.testInvoke(aiChat);
        return Result.success(ac);
    }

    //微信登录
    @PostMapping("/wechatLogin")
    public Result wechatLogin(String code,String nickName,String avatar) {
        String result=wechatService.login(code);
        JSONObject json= JSONUtil.parseObj(result);

        //如果res有错误码则不合法
        if(json.containsKey("errcode")){
            return Result.error("微信登录失败");
        }

        //没有则成功，提取openid
        String openId=json.getStr("openid");
        if(nickName==null&&avatar==null){
            return Result.success(openId);
        }

        //查询账号是否注册，没有则注册
        UserDTO user=userService.GetUserInfo(openId);
        if(user==null){
            user=wechatService.autoRegister(openId,nickName,avatar);
            return Result.success(user.getUID());
        }
        return Result.success(user.getUID());
    }

}
